package com.yidu.qianfeng.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yidu.qianfeng.common.bean.finance.Finance;
import com.yidu.qianfeng.common.bean.finance.FinanceCondition;
import com.yidu.qianfeng.common.bean.finance.Income;
import com.yidu.qianfeng.common.bean.waybill.WaybillCondition;
import com.yidu.qianfeng.common.util.LayuiJsonWrap;
import com.yidu.qianfeng.service.FinanceServiceInter;
import com.yidu.qianfeng.service.IncomeServiceInter;
import com.yidu.qianfeng.service.impl.FinanceServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: 戴超
 * @Description: 收入明细
 */
@Controller
public class IncomeController {
    @Resource(name = "incomeServiceImpl")
    private IncomeServiceInter incomeService;
    @Resource(name = "financeServiceImpl")
    private FinanceServiceInter financeService;

    @RequestMapping("/income")
    public String income(){
        return "finance/income";
    }
    /**
     * 分页查询收入
     * @param
     * @return
     */
    @RequestMapping("/selectAllIncome")
    @ResponseBody
    public Map selectAllIncome(@Param("page")int page, @Param("limit")int limit){
        List<Income> income = incomeService.selectAllIncome((page-1)*limit,limit);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",incomeService.selectIncomeCount());
        map.put("data",income);
        return map;
    }

    /**
     * 新增收入
     * @param income
     * @return
     */
    @PostMapping("/addIncome")
    @ResponseBody
    public String addIncome(@RequestBody Income income){
        UUID uuid = UUID.randomUUID();
        String str = uuid.toString();
        income.setIncomeId(str);
        Finance finance = new Finance();
        finance.setInspendId(str);
        //资金项目
        finance.setInspendFound("收入");
        //录入日期
        finance.setInspendDate(income.getIncomeEnteringDate());
        //支出金额
        finance.setInspendSpendmoney(null);
        //收入金额
        finance.setInspendIncomemoney(income.getIncomeCredit());
        //收支科目
        finance.setInspendCourse(income.getIncomeCourse());
        //收支类型
        finance.setInspendType(income.getIncomeType());
        //进港站点
        finance.setInspendArrivalStation(income.getIncomeArrivalStation());
        //发生站点
        finance.setInspendOccurStation(income.getIncomeSubstId());
        //收支方式
        finance.setInspendWay(income.getIncomeWay());
        //收支内容
        finance.setInspendContent(income.getIncomeContent());
        //收支操作员
        finance.setInspendOperator(income.getIncomeTypeinPerson());
        //付款对象
        finance.setInspendPayObj(income.getIncomePayObj());
        //收支备注
        finance.setInspendRemark(income.getIncomeRemark());
        //收支个人审核
        finance.setInspendPersonCheck("否");
        //收支审核人
        finance.setInspendVerifier("");
        //收支分站审核
        finance.setInspendSubstationCheck("否");
        //收支分站审核人
        finance.setInspendSubstationVerifier("");
        //凭证号
        finance.setInspendVoucherNum(income.getIncomeVoucherNum());
        financeService.insertFinance(finance);
        return incomeService.insertIncome(income);
    }

    /**
     * 根据条件查询收入明细
     * @return
     */
    @RequestMapping("/selectIncomeByCondition")
    @ResponseBody
    public LayuiJsonWrap selectIncomeByCondition(@RequestParam("financeCondition")String financeCondition){
        FinanceCondition condition = null;
        try {
            condition = new ObjectMapper().readValue(financeCondition, FinanceCondition.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        System.out.println(condition.getFinanceOccurStation());
        return incomeService.selectIncomeByCondition(condition);
    }
    /**
     * 根据分站ID查询操作站点
     */
    @RequestMapping("/selectSubNameById")
    @ResponseBody
    public String selectSubNameById(@Param("empSubstId") String empSubstId){
        return incomeService.selectSubNameById(empSubstId);
    }
}
